package problem41;

//41.缺失的第一个正数
//https://leetcode.cn/problems/first-missing-positive/?envType=study-plan-v2&envId=top-100-liked

class Solution {
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        for(int i = 0; i<n; i++) {
            while(nums[i] > 0 && nums[i] < n && nums[i] != nums[nums[i] - 1]) {
                int t = nums[nums[i] - 1];
                nums[nums[i] - 1] = nums[i];
                nums[i] = t;
            }
        }
        for(int i = 0; i<n; i++) {
            if(nums[i] != i + 1) return i+1;
        }
        return n+1;
    }
}